﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICR
    {

        public DataSet CR_GetAllByRecruitmentID(int RecruitmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_GetAllByRecruitmentID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Recruitment_ID", RecruitmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet CR_GetList(out int ItemCount, int _Status, string KeyWord, int RecruitmentID, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Status", _Status);
            dbCmd.Parameters.AddWithValue("@KeyWord", KeyWord);
            dbCmd.Parameters.AddWithValue("@RecruitmentID", RecruitmentID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public CRInfo CR_GetInfo(int _CR_ID)
        {
            CRInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CR_ID", _CR_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CRInfo();
                    retVal.ID = Convert.ToInt32(dr["CR_ID"]);
                    retVal.RecruitmentID = Convert.ToInt32(dr["CR_RecruitmentID"]);
                    retVal.CandidateID = Convert.ToInt32(dr["CR_CandidateID"]);
                    retVal.Year = Convert.ToInt32(dr["CR_Year"]);
                    retVal.Status = dr["CR_Status"] == DBNull.Value ? 0 : Convert.ToInt32(dr["CR_Status"]);

                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int CR_Insert(CRInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CR_RecruitmentID", eInfo.RecruitmentID);
            dbCmd.Parameters.AddWithValue("@CR_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CR_Year", eInfo.Year);
            dbCmd.Parameters.AddWithValue("@CR_Status", eInfo.Status);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CR_Update(CRInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CR_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@CR_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CR_RecruitmentID", eInfo.RecruitmentID);
            dbCmd.Parameters.AddWithValue("@CR_Year", eInfo.Year);
            dbCmd.Parameters.AddWithValue("@CR_Status", eInfo.Status);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CR_Delete(int CRID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CR_ID", CRID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CR_Exist( int _ID, int CandidateID,int RecruitmentID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CR_ID", _ID);
            dbCmd.Parameters.AddWithValue("@CR_CandidateID", CandidateID);
            dbCmd.Parameters.AddWithValue("@CR_RecruitmentID", RecruitmentID);

            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CR_CandidateMatriculate(int CR_ID, int Canidate_ID, int Status)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateRecruitment_CandidateMatriculate", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CR_ID", CR_ID);
            dbCmd.Parameters.AddWithValue("@CR_CandidateID", Canidate_ID);
            dbCmd.Parameters.AddWithValue("@CR_Status", Status);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
